/*
 * @Author: liyongxuan
 * @Date: 2018-1-10 10:37:42
 * @Last Modified by: liyongxuan
 * @Last Modified time: 2018-1-10 10:37:42
 * @description项目管理-拟投资-投票结果组件
 */
<template>
    <div class="ca-m-t-15">
        <div class="right-fs-30">
            <el-button @click="addVot()" :disabled="addMeetingBtnAuthor()" type="primary"><i class="el-icon-plus"></i>新增{{title}}</el-button>
        </div>
        <div class="clear-both"></div>
        <csVoteAdd @close="closeAdd()" v-if="voteEditFlag" :itemId="itemId" :type="type" :fileType="fileType" v-model="resultData"></csVoteAdd>        
        <div>
            <div v-for="(itemData,index) in resultData" :key="index"  class="ca-box ca-p-22 ca-m-t-15">
                <div v-if="!itemData.isEdit">                    
                    <div class="ca-title">
                        <h3>投票结果</h3>
                        <div align="right">
                            <el-button type="text" class="ca-edit-btn el-icon-edit" v-if="tabAuthFn()" @click="editVote(itemData,index)">编辑</el-button>                
                        </div>
                    </div>
                    <el-row class="ca-m-t-10">
                        <el-col :span="3">
                            <span class="ca-span-title">投票结果：</span>
                            <span class="cv-result-tag fn-color">{{itemData.voteResult||'-'}}</span>
                        </el-col>
                        <el-col :span="4">
                            <span class="ca-span-title">票数通过率：</span>
                            <span class="cv-result-tag fn-color">{{itemData.ticketNumberPassingRate|format1}}%</span>
                        </el-col>
                        <el-col :span="3">
                            <span class="ca-span-title">已投人数：</span>
                            <span class="cv-result-tag fn-color">{{itemData.votedNum||'-'}}</span>
                        </el-col>
                        <el-col :span="3">
                            <span class="ca-span-title">未投人数：</span>
                            <span class="cv-result-tag fn-color">{{itemData.noVoteVetoed||'-'}}</span>
                        </el-col>
                        <el-col :span="3">
                            <span class="ca-span-title">赞同票：</span>
                            <span class="cv-result-tag fn-color">{{itemData.agreeNum||'-'}}</span>
                        </el-col>
                        <el-col :span="3">
                            <span class="ca-span-title">反对票：</span>
                            <span class="cv-result-tag fn-color">{{itemData.againstNum||'-'}}</span>
                        </el-col>
                    </el-row>
                    <el-row>
                        <table class="ca-table ca-m-t-15 ca-tale-th-pad">
                            <tr>
                                <th width="15%">投票人</th>
                                <th width="13%">角色</th>
                                <th width="10%">投票</th>
                                <th width="35%">意见</th>
                                <th width="20%">投票时间</th>
                                <th width="7%">审批</th>
                            </tr>
                            <tr class="line-height" v-for="(item,indexVote) in itemData.quasiVoteUserVoList" :key="indexVote">
                                <td>{{item.voteUserName}}</td>
                                <td>{{item.voteRole|getVoteRole}}</td>
                                <td>{{item.userVoteResult|getUserVoteResult}}</td>
                                <td class="opinion-height">{{item.opinion}}</td>
                                <td>{{(item.voteDate==null||item.voteDate=='')?'':$moment(item.voteDate).format('YYYY-MM-DD hh:mm:ss A')}}</td>
                                <td>
                                    <el-button @click="voteDialog(item,index,indexVote)" type="primary" :disabled="voteAuthor(item)" size="small">投票</el-button>
                                </td>
                            </tr>
                        </table>
                    </el-row>
                    <el-row class="ca-m-t-20">
                        <el-col :span="12" class="ca-title">
                            <h3>会议决议</h3><br/>
                        </el-col>
                    </el-row>
                    <el-row class="ca-m-t-15">
                        <el-col :span="24" class="bak">{{itemData.remark}}</el-col>
                    </el-row>
                    <csUploadFileList class="ca-m-t-20" :fileListBak="itemData.filesVoList" :businessId="itemData.id" :fileType="fileType"></csUploadFileList>
                </div>
                <csVoteEdit @closeEdit="closeEdit(itemData,index)" v-if="itemData.isEdit" :itemId="itemId" :proMan="proMan" :type="type" :fileType="fileType" :itemData="itemData"></csVoteEdit>                
            </div>
        </div>
        <!-- 投票弹窗 -->
        <el-dialog title="投票" :visible.sync="dialogVoteVisible" width="30%">
          <el-form :model="voteForm">            
              <el-form-item label="投票" required label-width="60px">
              <el-select v-model="voteForm.userVoteResult" placeholder="请选择">
                  <el-option v-for="(x,index) in initView.userVoteResult" :label="x.name" :value="x.id" :key="index"></el-option>
              </el-select>
              </el-form-item>
              <el-form-item label="意见" label-width="60px">
              <el-input v-model="voteForm.opinion" placeholder="不超过3000字" :maxlength="3000"  type="textarea" :rows="6"></el-input>
              </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
              <el-button @click="dialogVoteVisible = false">取 消</el-button>
              <el-button type="primary" @click="voteSave">确 定</el-button>
          </div>
      </el-dialog>
    </div>
</template>

<script type="text/ecmascript-6">
    import { api } from '../../api';
    import { apiHttp, STATUS_HTTP_SUCCESS } from '__service/csHttp';
    import { addRemind,isNoAuth } from '__service/utils';
    export default {
        data() {
            return {
                meetingAuthor: '',
                userId: this.$store.state.user.id,//用户id
                voteDialogData: {},//修改或保存投票参数
                addVoteFlag: false,//添加列表行
                voteEditFlag: false,//新建flag
                dialogVoteVisible: false,//投票弹窗flag
                opinionFlag: false,//意见flag
                opinionEdit: '',//编辑意见
                resultData: [],//查询返回数据
                voteForm: {},//投票弹窗
                indexAll: null,
                indexVote: null,
                initView: {
                    userVoteResult: [{id: 0,name: '请选择'},{id: 1,name: '赞同'},{id: 2,name: '反对'},{id: 3,name: '弃权'},{id: 4,name: '一票通过'},{id: 5,name: '一票否决'}],
                    userList: this.$store.state.user.department.userEntityList,//角色
                    voteRole: [{id: 1,name: '内部'},{id: 2,name: '外部'}],
                },
            }
        },
        filters: {
            getUserVoteResult(value) {
                let userVoteResult = [{id: 0,name: '未投票'},{id: 1,name: '赞同'},{id: 2,name: '反对'},{id: 3,name: '弃权'},{id: 4,name: '一票通过'},{id: 5,name: '一票否决'}]
                for (let u of userVoteResult) {
                    if (u.id === value) {
                        return u.name
                    }
                }
            },
            getVoteRole(value) {
                let voteRole = [{id: 1,name: '内部'},{id: 2,name: '外部'}]
                for (let u of voteRole) {
                    if (u.id === value) {
                        return u.name
                    }
                }
            }
        },
        props: ['title','itemId','type','fileType','proMan'],
        mounted() {
            this.getQuasiVoteEntity()
            if (this.type === '1') {
                this.meetingAuthor = '拟投资项目-立项-编辑'
            } else if (this.type === '2') {
                this.meetingAuthor = '拟投资项目-风控会-编辑'
            } else if (this.type === '3') {
                this.meetingAuthor = '拟投资项目-投决会-编辑'
            }
        },
        methods: {
            // 编辑会议按钮
            editVote(itemData,index) {
                itemData.isEdit = true
                this.$set(this.resultData,index,itemData)
            },
            // 查询投票信息
            async getQuasiVoteEntity() {
                const {resCode,data} = await apiHttp(api.GET_QUASI_VOTE_ENTITY, {itemId: this.itemId,type: this.type})
                if (resCode === STATUS_HTTP_SUCCESS) {
                    let voteResultAll = []
                    this.resultData = data
                    this.resultData.map((u,index) => {
                        voteResultAll.push({
                            voteResult: '-',//投票结果
                            ticketNumberPassingRate: 0,//票数通过率
                            votedNum: 0,//已投人数
                            agreeNum: 0,//赞同数
                            againstNum: 0,//反对人数
                            noVoteVetoed: 0//未投人数
                        })
                        if (u.quasiVoteUserVoList.length > 0) {
                            u.quasiVoteUserVoList.map(item => {
                                if (item.userVoteResult === 2 || item.userVoteResult === 5) {
                                    voteResultAll[index].againstNum += 1
                                } else if (item.userVoteResult === 1 || item.userVoteResult === 4) {
                                    voteResultAll[index].agreeNum += 1
                                } else if (item.userVoteResult === 0 || item.userVoteResult === 3) {
                                    voteResultAll[index].noVoteVetoed += 1
                                }
                            })
                        }
                        u.voteResult = '' //投票结果
                        if (u.quasiVoteUserVoList.length === 0) {
                            u.ticketNumberPassingRate = '-'
                        } else {
                            u.ticketNumberPassingRate = ((voteResultAll[index].agreeNum / u.quasiVoteUserVoList.length) * 100)//票数通过率
                            if (voteResultAll[index].agreeNum / u.quasiVoteUserVoList.length >= 0.5) {
                                u.voteResult = '通过'
                            } else {
                                u.voteResult = '不通过'
                            }
                        }
                        u.againstNum = voteResultAll[index].againstNum //反对人数
                        u.agreeNum = voteResultAll[index].agreeNum //赞同数
                        u.noVoteVetoed = voteResultAll[index].noVoteVetoed //一票否决
                        u.votedNum = u.quasiVoteUserVoList.length - u.noVoteVetoed //已投人数
                        u.isEdit = false
                    })
                }
            },
            // 已经投过票的用户不显示在下拉列表内
            getUserList(index) {
                let list = this.initView.userList;
                let resultData = this.resultData
                for (let i = 0; i < this.resultData[index].quasiVoteUserVoList.length; i++) {
                    list = list.filter(x => {
                        return resultData[index].quasiVoteUserVoList[i].userId !== x.id
                    })
                }
                return list;
            },
            // 增加按钮
            addVot() {
                this.voteEditFlag = true
            },
            // 关闭新增
            closeEdit(itemData,index) {
                this.getQuasiVoteEntity()
                itemData.isEdit = false
                this.$set(this.resultData,index,itemData)
            },
            // 关闭新增
            closeAdd() {
                this.getQuasiVoteEntity()
                this.voteEditFlag = false
            },
            // 投票按钮
            voteDialog(item,index,indexVote) {
                this.dialogVoteVisible = true
                this.indexAll = index
                this.indexVote = indexVote
                this.voteForm = {
                    'id': item.id || '',
                    'opinion': item.opinion,
                    'voteDate': item.voteDate,
                    'userId': item.userId,
                    'userVoteResult': item.userVoteResult,
                    'voteRole': item.voteRole
                }
            },
            // 投票弹窗保存按钮
            async voteSave() {
                this.voteForm.voteDate = new Date()
                if (this.voteForm.userVoteResult === 0) {
                    this.$message({type: 'error',message: '请选择投票类型',center: true})
                    return false
                }
                const {resCode} = await apiHttp(api.PUT_QUASI_VOTE_ENTITY,this.voteForm)
                if (resCode === STATUS_HTTP_SUCCESS) {
                    this.getQuasiVoteEntity()
                    this.dialogVoteVisible = false
                }
            },
            // 添加小红点
            async addRed() {
                let query = {
                    dataId: this.itemId || '', // 业务id
                    dataTab: 1, // tab页顺序 head 也为0
                    dataType: 1, // 1拟投资项目,2已投资项目,3母基金,4拟设立基金,5已设立基金,6客户,7潜在项目
                }
                if (this.type === '1') {
                    query.dataTab = 1
                } else if (this.type === '2') {
                    query.dataTab = 3
                } else if (this.type === '3') {
                    query.dataTab = 4
                }
                await addRemind(query);
                // 根据第三方组件来调用 父组件中定义的方法 重新获取小红点
                this.$root.Dot.$emit('getDotList');
            },
            // 权限
            tabAuthFn(flag = 'edit') {
                let item = '';
                if (flag === 'control') {
                    if (this.type === '1') {
                        item = '拟投资项目-立项-投票控制'
                    } else if (this.type === '2') {
                        item = '拟投资项目-风控会-投票控制'
                    } else if (this.type === '3') {
                        item = '拟投资项目-投决会-投票控制'
                    }
                } else {
                   if (this.type === '1') {
                        item = '拟投资项目-立项-编辑'
                    } else if (this.type === '2') {
                        item = '拟投资项目-风控会-编辑'
                    } else if (this.type === '3') {
                        item = '拟投资项目-投决会-编辑'
                    }
                }
                return isNoAuth(item)
            },
            // 新增会议按钮权限
            addMeetingBtnAuthor() {
                if (this.tabAuthFn() || this.userId === this.proMan) {
                    return false
                } else {
                    return true
                }
            },
            // 投票按钮权限
            voteAuthor(item) {
                if (this.tabAuthFn('control') && this.userId === item.userId && item.userVoteResult === 0 || this.userId === '1') {
                    return false
                } else {
                    return true
                }
            },
        },
        components: {
            csVoteEdit: require('./edit'),
            csVoteAdd: require('./add'),
            csFileList: require('__components/file-list/index'),//文件列表
            csUploadFileList: require('../../upload/upload'),// 上传文件
        },
    }
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.right-fs-30{
    float:right;
}
.fn-color{
    color: #00AFFF
}
.ca-tale-th-pad th {
    padding: 0px
}
.clear-both{
    clear: both;
}
.line-height{
    line-height: 24px;
}
.line-height td{
    vertical-align: middle;
}
.opinion-height{
    max-height: 64px;
    overflow: auto;
    display: inline-block;
    width: 100%
}
.bak{
    word-wrap: break-word
}
</style>
